//
//  FSWSelectBottomView.m
//  voicetip_ios
//
//  Created by 伏圣文 on 2024/7/24.
//  Copyright © 2024 fusw. All rights reserved.
//

#import "FSWSelectBottomView.h"
#import "FSWSelectBottomCell.h"

@interface FSWSelectBottomView()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *backHeightConstraint;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) NSMutableArray *list;

@end


@implementation FSWSelectBottomView

+ (instancetype)footerView{
   return [[[NSBundle mainBundle] loadNibNamed:@"FSWSelectBottomView" owner:nil options:nil] lastObject];
}
 
-(void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 50;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    }
    [self.tableView registerNib:[UINib nibWithNibName:@"FSWSelectBottomCell" bundle:nil] forCellReuseIdentifier:@"listCell"];
}
  
- (void)show:(NSMutableArray*_Nullable)model{
    [self.list removeAllObjects];
    [self.list addObjectsFromArray:model];
    [self.tableView reloadData];
   if (self.titleStr.length > 0) {
       self.titleLabel.text = self.titleStr;
   }
   [self showTableHeigth];
   [self showWindow];
}
  

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
   return self.list.count;
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    @weakify(self);
    FSWSelectBottomCell* cell = [tableView dequeueReusableCellWithIdentifier:@"listCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell configUI:self.list[indexPath.row]];
 
    return cell;
}


#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
 

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
   return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
   return [UIView new];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.type == 1){
        FSWSelectModel *model = self.list[indexPath.row];
        if ([model.isSelect integerValue] == 1) {
            model.isSelect = @"0";
        }else{
            model.isSelect = @"1";
        }
    }else{
        if (self.isnil == 1) {
            for (int i=0; i< self.list.count; i++) {
                FSWSelectModel *model = self.list[i];
                if (i == indexPath.row) {
                    if ([model.isSelect integerValue] == 1) {
                        model.isSelect = @"0";
                    }else{
                        model.isSelect = @"1";
                    }
                }else{
                    model.isSelect = @"0";
                }
            }
        }else{
            for (int i=0; i< self.list.count; i++) {
                FSWSelectModel *model = self.list[i];
                if (i == indexPath.row) {
                    model.isSelect = @"1";
                }else{
                    model.isSelect = @"0";
                }
            }
        }
    }
    [self.tableView reloadData];
     
 
}

-(void)showTableHeigth{
    [self.tableView reloadData];
    float sh = SCREEN_HEIGHT *0.5;
    self.backHeightConstraint.constant = sh;
}
- (IBAction)closeButton:(id)sender {
    @weakify(self);
    [self dismiss:^(BOOL finished) {
        @strongify(self);
        [self cancleWindow];
    }];
}

- (void)showWindow
{
   @weakify(self);
   [[UIApplication sharedApplication].keyWindow addSubview:self];
   self.windowLevel = UIWindowLevelAlert;
   self.frame = [UIScreen mainScreen].bounds;
   [UIView animateWithDuration:0.5
                         delay:0
        usingSpringWithDamping:1.0
         initialSpringVelocity:1.0
                       options:UIViewAnimationOptionCurveLinear
                    animations:^{
   }
                    completion:^(BOOL finished) {
       @strongify(self);
       self.userInteractionEnabled = YES;
   }];
}

- (void)cancleWindow
{
   self.userInteractionEnabled = NO;
   [self resignKeyWindow];
   [self setHidden:YES];
   [[UIApplication sharedApplication].keyWindow makeKeyWindow];
}

- (void)dismiss:(void (^)(BOOL))completion
{
   [UIView animateWithDuration:0.2
                         delay:0
        usingSpringWithDamping:0.9
         initialSpringVelocity:0.9
                       options:UIViewAnimationOptionCurveLinear
                    animations:^{
   }
                    completion:completion];
}
- (IBAction)okButton:(id)sender {
    @weakify(self);
    [self dismiss:^(BOOL finished) {
        @strongify(self);
        [self cancleWindow];
        NSMutableArray *array = [NSMutableArray array];
        for (int i=0; i< self.list.count; i++) {
            FSWSelectModel *model = self.list[i];
            if ([model.isSelect integerValue] == 1) {
                [array addObject:model];
            }
        }
        if (self.selectBlock) {
            self.selectBlock(array);
        }
    }];
}

-(NSMutableArray *)list{
    if (!_list) {
        _list = [NSMutableArray array];
    }
    return _list;
}


@end
